import fileService from '../../../api/file'
import { Message } from 'element-ui'

const file = {
  namespaced: true,
  state: {
    parentId: '',
    defaultParentId: '',
    defaultParentFilename: '',
    fileList: [],
    multipleSelection: [],
    fileTypes: '-1',
    searchFlag: false,
    searchShow: false,
    searchKey: '',
    tableLoading: true,
    tableType: 2,
    uploadDialogVisible: false,
    spaceSize: 0,
    userSpace: 0,
    createFolderDialogVisible: false,
    contextMenuVisible: false,
  },
  mutations: {
    setUploadShow(state, uploadDialogVisible) {
      state.uploadDialogVisible = uploadDialogVisible
      state.contextMenuVisible = false
    },
    setCreateFolderDialogVisible(state, createFolderDialogVisible) {
      state.createFolderDialogVisible = createFolderDialogVisible
      state.contextMenuVisible = false
    },
    setContextMenuVisible(state, contextMenuVisible) {
      state.contextMenuVisible = contextMenuVisible
    },
    setSearchShow(state, searchShowFlag) {
      if (arguments[1] !== undefined) {
        state.searchShow = arguments[1]
      } else {
        state.searchShow = !state.searchShow
      }
    },
    setFileParentId(state, parentId) {
      state.parentId = parentId
    },
    refreshFileParentId(state) {
      state.parentId = state.defaultParentId
    },
    setFileDefaultParentId(state, defaultParentId) {
      state.defaultParentId = defaultParentId
    },
    setDefaultParentFilename(state, defaultParentFilename) {
      state.defaultParentFilename = defaultParentFilename
    },
    setFileList(state, fileList) {
      state.fileList = fileList
    },
    setMultipleSelection(state, multipleSelection) {
      state.multipleSelection = multipleSelection
    },
    setFileTypes(state, fileTypes) {
      state.fileTypes = fileTypes
    },
    setSearchFlag(state, searchFlag) {
      if (!searchFlag) {
        state.searchKey = ''
      }
      state.searchFlag = searchFlag
    },
    setSearchKey(state, searchKey) {
      state.searchKey = searchKey
    },
    setTableLoading(state, tableLoading) {
      state.tableLoading = tableLoading
    },
    clear(state) {
      state.parentId = ''
      state.defaultParentId = ''
      state.defaultParentFilename = ''
      state.fileList = new Array()
      state.multipleSelection = new Array()
      state.fileTypes = '-1'
      state.searchFlag = false
      state.searchKey = ''
      state.tableLoading = true
    },
    changeListType(state) {
      state.tableType = state.tableType === 1 ? 2 : 1
    },
    setUserSpace(state, data) {
      state.userSpace = data.userSpace
      state.spaceSize = data.spaceSize
    },
  },
  actions: {
    loadFileList({ commit, state, rootState, dispatch }) {
      commit('setTableLoading', true)
      commit('setUploadShow', false)
      commit('setCreateFolderDialogVisible', false)
      if (state.searchFlag && state.searchKey) {
        fileService.search(
          {
            keyword: state.searchKey,
            fileTypes: '-1',
          },
          (res) => {
            commit('setFileList', res.data)
            commit('setTableLoading', false)
            commit('setMultipleSelection', new Array())
            commit('setUploadShow', false)
          },
          (res) => {
            commit('setTableLoading', false)
            Message.error(res.message)
          }
        )
      } else {
        fileService.list(
          {
            parentId: state.parentId,
            fileTypes: state.fileTypes,
          },
          (res) => {
            commit('setTableLoading', false)
            commit('setFileList', res.data)
            commit('setMultipleSelection', new Array())
          },
          (res) => {
            commit('setTableLoading', false)
            Message.error(res.message)
          }
        )
      }
    },
  },
}

export default file
